package com.qifan.energy.activity;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.qifan.energy.base.BaseActivity;
import com.qifan.energy.R;
import com.qifan.energy.adapter.HotProductListAdapter;
import com.qifan.energy.adapter.NewProductListAdapter;
import com.qifan.energy.bean.result.HomeListResult;
import com.qifan.energy.bean.result.Page2;
import com.qifan.energy.global.Keys;
import com.qifan.energy.http.net.NetUtils;
import com.qifan.energy.http.response.MyObserver;
import com.qifan.energy.util.IntentUtils;
import com.qifan.energy.util.ToastUtils;
import com.qifan.energy.widget.CustomTitleBar;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class GoodsListActivity extends BaseActivity {

    @BindView(R.id.title_layout_bar)
    CustomTitleBar titleBar;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    private HotProductListAdapter hotProductListAdapter;
    private NewProductListAdapter newProductListAdapter;
    private Integer pageNum = 1;//默认1

    List<HomeListResult.DistributePmsProductListBean> dataList1 = new ArrayList<>();
    List<HomeListResult.NormalPmsProductListBean> dataList2 = new ArrayList<>();

    /*@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView();
    }*/

    @Override
    protected int getLayoutId() {
        return R.layout.activity_goods_list;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        /* 取值 */
        int type = getIntent().getIntExtra(Keys.HOME_SHOW_TYPE,0);
        switch (type){
            case 1:
                /* 适配器设置 */
                hotProductListAdapter = new HotProductListAdapter(dataList1);
                hotProductListAdapter.setOnItemClickListener((a,v,p) -> {
                    HomeListResult.DistributePmsProductListBean item = (HomeListResult.DistributePmsProductListBean)a.getData().get(p);
                    Intent intent = new Intent();
                    intent.putExtra(Keys.DETAIL_ID_KEY,item.getId());
                    IntentUtils.startActivity(this, GoodsDetailActivity.class,intent);
                });
                //recyclerView.setLayoutManager(new LinearLayoutManager(this));
                final GridLayoutManager layoutManager = new GridLayoutManager(this,2);
                layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                recyclerView.setLayoutManager(layoutManager);
                recyclerView.setAdapter(hotProductListAdapter);
                recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this)
                        .sizeResId(R.dimen.dp_1)
                        .colorResId(R.color.divider_color)
                        .build());
                //添加空布局
                hotProductListAdapter.setEmptyView(getEmptyDataView());
                /* 加载更多 */
                hotProductListAdapter.setOnLoadMoreListener(()->{
                    homeHotProductListMore();
                },recyclerView);
                break;
            case 2:
                /* 适配器设置 */
                newProductListAdapter = new NewProductListAdapter(dataList2);
                newProductListAdapter.setOnItemClickListener((a,v,p) -> {
                    HomeListResult.NormalPmsProductListBean item = (HomeListResult.NormalPmsProductListBean)a.getData().get(p);
                    Intent intent = new Intent();
                    intent.putExtra(Keys.DETAIL_ID_KEY,item.getId());
                    IntentUtils.startActivity(this, GoodsDetailActivity.class,intent);
                });
                recyclerView.setLayoutManager(new LinearLayoutManager(this));
                recyclerView.setAdapter(newProductListAdapter);
                recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this)
                        .sizeResId(R.dimen.dp_1)
                        .colorResId(R.color.divider_color)
                        .build());
                //添加空布局
                newProductListAdapter.setEmptyView(getEmptyDataView());
                /* 加载更多 */
                newProductListAdapter.setOnLoadMoreListener(()->{
                    homeNewProductListMore();
                },recyclerView);
                break;
            case 3:
                break;
            default:
                break;
        }
    }

    /* 获取空布局 */
    private View getEmptyDataView() {
        return getLayoutInflater().inflate(R.layout.product_list_empty, recyclerView, false);
    }

    @Override
    protected void initData() {
        /* 取值 */
        int type = getIntent().getIntExtra(Keys.HOME_SHOW_TYPE,0);
        switch (type){
            case 1:
                titleBar.setTitleBarTitle("分销商品区");
                homeHotProductList();
                break;
            case 2:
                titleBar.setTitleBarTitle("普通商品区");
                homeNewProductList();
                break;
            case 3:
                titleBar.setTitleBarTitle("特色商品");
                break;
            default:
                break;
        }
    }

    /* 分销商品区列表接口(下拉刷新) */
    private void homeHotProductList(){
        pageNum = 1;
        NetUtils.homeDistributeProductList(pageNum,Page2.PAGESIZE_10,new MyObserver<Page2<HomeListResult.DistributePmsProductListBean>>(this) {

            @Override
            public void onSuccess(Page2<HomeListResult.DistributePmsProductListBean> result) {
                if (null != result) {
                    dataList1 = result.getList();
                    hotProductListAdapter.setNewData(result.getList());
                }
            }

            @Override
            public void onFailure(Throwable e, String errorMsg) {
                //提示
                ToastUtils.showShort(errorMsg);
            }
        });
    }

    /* 分销商品区列表接口(上拉加载) */
    private void homeHotProductListMore(){
        ++pageNum;
        NetUtils.homeDistributeProductList(pageNum,Page2.PAGESIZE_10,new MyObserver<Page2<HomeListResult.DistributePmsProductListBean>>(this) {

            @Override
            public void onSuccess(Page2<HomeListResult.DistributePmsProductListBean> page2) {
                if (null != page2) {
                    dataList1.addAll(page2.getList());
                    boolean isNoMore = (dataList1.size() >= page2.getTotal());
                    hotProductListAdapter.setEnableLoadMore(!isNoMore);
                    if (isNoMore) {
                        hotProductListAdapter.loadMoreEnd();
                    } else {//刷新数据
                        hotProductListAdapter.setNewData(dataList1);
                    }
                }
            }

            @Override
            public void onFailure(Throwable e, String errorMsg) {
                //提示
                ToastUtils.showShort(errorMsg);
            }
        });
    }

    /* 普通商品区列表接口(下拉刷新) */
    private void homeNewProductList(){
        pageNum = 1;
        NetUtils.homeNormalProductList(pageNum, Page2.PAGESIZE_10, new MyObserver<Page2<HomeListResult.NormalPmsProductListBean>>(this) {

            @Override
            public void onSuccess(Page2<HomeListResult.NormalPmsProductListBean> result) {
                if (null != result) {
                    dataList2 = result.getList();
                    newProductListAdapter.setNewData(dataList2);
                }
            }

            @Override
            public void onFailure(Throwable e, String errorMsg) {
                //提示
                ToastUtils.showShort(errorMsg);
            }
        });
    }

    /* 普通商品区列表接口(上拉加载) */
    private void homeNewProductListMore(){
        ++pageNum;
        NetUtils.homeNormalProductList(pageNum, Page2.PAGESIZE_10, new MyObserver<Page2<HomeListResult.NormalPmsProductListBean>>(this) {

            @Override
            public void onSuccess(Page2<HomeListResult.NormalPmsProductListBean> page2) {
                if (null != page2) {
                    dataList2.addAll(page2.getList());
                    boolean isNoMore = (dataList2.size() >= page2.getTotal());
                    newProductListAdapter.setEnableLoadMore(!isNoMore);
                    if (isNoMore) {
                        newProductListAdapter.loadMoreEnd();
                    } else {//刷新数据
                        newProductListAdapter.setNewData(dataList2);
                    }
                }
            }

            @Override
            public void onFailure(Throwable e, String errorMsg) {
                //提示
                ToastUtils.showShort(errorMsg);
            }
        });
    }
}
